//
//  AppDelegate.swift
//  weibo
//
//  Created by uncle-R on 15/12/15.
//  Copyright © 2015年 uncle-R. All rights reserved.
// 3585513126
//815758ea90dd5568b7bbe7f53790b055
//https://api.weibo.com/oauth2/authorize?client_id=3585513126&redirect_uri=http://www.baidu.com/response&response_type=code
import UIKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        
        window = UIWindow(frame:UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        
//        //test: 加载可用账号
//        let userAccount = RXUserAccount.loadUserAccount()
//        print("userAccount\(userAccount)")
        
        // 设置导航栏按钮的外观
        setupAppearance()
        
        
//        let mainBar = RXMainViewController()
        // ?: 表示?前面的有值就执行?号后面的代码,
        // ?前面的为nil不执行后面的代码, 整个表达式返回nil
//       let future =  RXNewFeatureController()
        // test 欢迎界面
//         window?.rootViewController = RXWellcomeController()
        
        
        //返回让默认的初始控制器判断当前的控制器是哪个.
        window?.rootViewController = defaultViewController()
        //测试 CollectionView的新特性界面.
//        let test = RXFeatureViewController()
   
        window?.makeKeyAndVisible()
        
        return true
    }

    //设置全局外观
    private func setupAppearance() {
        let bar = UINavigationBar.appearance()
        bar.tintColor = UIColor.orangeColor()
    }
    /// 判断程序启动后显示的默认控制器
    
    private func defaultViewController() -> UIViewController {
        
        //判断用户是否登录
        if !RXUserAccount.userIslogin{ //如果没有登录.加载访客界面.
            return RXMainViewController()
        }
        //用户已经登录
        //判断是否是新版本
        if  isNewVersion() { //加载新特性界面
            return RXFeatureViewController()
        }
        return RXWellcomeController()
    }
 
    
    func switchRootViewController(isMain:Bool) -> Void{
        
     return   (window?.rootViewController = isMain ? RXMainViewController() : RXWellcomeController())!
    }
    
    
    
    
    //MARK: - 判断是否最新版本号
    
    private func isNewVersion() -> Bool{
        //1.获取当前版本号
        
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        
        print("当前版本: \(currentVersion)")
        
        //2.获取上一次保存的版本
        let sanBoxVersionKey = "sandboxVersionKey"
        let sanBoxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sanBoxVersionKey)
        print("沙盒的版本号:\(sanBoxVersion)")
        //3.保存当前版本
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: "sandboxVersionKey")
        
        NSUserDefaults.standardUserDefaults().synchronize()
        return currentVersion != sanBoxVersion
        
    }
    
    
    
    
    
    
    
    
}

